import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';

class BaseNestedScrollView extends StatelessWidget {

  /// 顶部的部件( 随滚动条的滚动而隐藏 )
  final Widget headerWidget;

  /// 中间悬浮固定的的部件( 随滚动条的滚动而固定在最顶部 )
  final BaseNestedScrollPinned pinnedWidget;

  /// 内容组件
  final Widget contentWidget;

  BaseNestedScrollView({Key key, this.headerWidget, @required this.pinnedWidget, @required this.contentWidget,})  : assert(pinnedWidget != null), assert(contentWidget != null), super(key: key);

  @override
  Widget build(BuildContext context) {
    return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
          SliverToBoxAdapter(child: this.headerWidget == null ? SizedBox() : this.headerWidget),
          SliverPersistentHeader(pinned: true, delegate: CustomSliverPersistentHeaderDelegate(child: pinnedWidget)),
          // _buildPinnedWidget(context, innerBoxIsScrolled),
        ];
      },
      body: this.contentWidget,
    );
  }

}

class BaseNestedScrollPinned extends StatelessWidget {

  final double height;
  final double maxExtent;
  final double minExtent;


  final Widget child;

  const BaseNestedScrollPinned({Key key, this.height = 48, @required this.child, this.maxExtent, this.minExtent}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(height: height, child: child);
  }

}

class CustomSliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {

  final BaseNestedScrollPinned child;

  CustomSliverPersistentHeaderDelegate({@required this.child}): assert(child != null), super();

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      decoration: BoxDecoration(
        color: shrinkOffset > 0 ? Colors.white : Colors.transparent,
        boxShadow: [
          BoxShadow(offset: Offset(0, 1.6), color: shrinkOffset > 0 ? BaseBodyTheme.greyBackgroundColor : Colors.transparent, blurRadius: 8)
        ],
      ),
      child: this.child,
    );
  }

  @override
  double get maxExtent => child.maxExtent == null ? child.height : child.maxExtent; // 返回最大高度

  @override
  double get minExtent => child.minExtent == null ? child.height : child.minExtent; // 返回最小高度

  @override
  bool shouldRebuild(CustomSliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }

}
